package com.stylefeng.guns.command.impl;

import com.stylefeng.guns.command.ICommand;
import com.stylefeng.guns.common.persistence.model.Barrage;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.concurrent.TimeUnit;

@Slf4j
public class SendCmd extends ICommand {

    private Barrage barrage;

    public SendCmd(Barrage barrage) {
        this.barrage = barrage;
    }

    @Override
    public void execute() {

        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
        ChromeOptions options = new ChromeOptions();

        options.addArguments("--headless");
        // Bypass OS security modelAutomationExtension", false);
        options.addArguments("--no-sandbox");

        WebDriver driver = new ChromeDriver(options);
        try {
            String url = barrage.getUrl();
            String username = barrage.getUsername();
            String password = barrage.getPassword();
            Integer times = barrage.getTimes();
            String message = barrage.getMessage();

            // 登录直播间
            login(driver, url, username, password);

            TimeUnit.SECONDS.sleep(2);

//            driver.get(url);
            driver.switchTo().defaultContent();
            int runTimes = 0;
            while (runTimes < times && !Thread.currentThread().isInterrupted()) {
                sendMessage(driver, message, runTimes);
                runTimes++;
            }
            driver.quit();

        } catch (Exception e) {
            log.error("线程={}执行出错", Thread.currentThread().getName(), e);
            driver.quit();
        }

    }

    private void sendMessage(WebDriver driver, String message, int runTimes) throws InterruptedException {

        WebElement inputElement = driver.findElement(By.id("pub_msg_input"));
        WebElement sendElement = driver.findElement(By.id("msg_send_bt"));
        if (runTimes > 0 && runTimes % 5 == 0) {
            inputElement.sendKeys("????");
            TimeUnit.MILLISECONDS.sleep(10);
            sendElement.click();
            TimeUnit.SECONDS.sleep(21);
        }
        // 输入信息
        inputElement.sendKeys(message);
        TimeUnit.MILLISECONDS.sleep(1000);
        sendElement.click();
        log.info("发送弹幕={}发送成功", message);


    }

    private static void login(WebDriver driver, String url, String username, String password) throws InterruptedException {

        driver.get(url);
        TimeUnit.SECONDS.sleep(2);
        log.info(driver.getTitle());
//        log.info(driver.getPageSource());

        By by = By.id("yyliveRk_game_newsBut");
        driver.findElement(by).click();
        log.info(driver.getCurrentUrl());
        TimeUnit.SECONDS.sleep(5);

//        WebElement iframe = driver.findElement(By.id("udbsdk_frm_normal"));
        WebDriver iframeDriver = driver.switchTo().frame("udbsdk_frm_normal");
        log.info(iframeDriver.getTitle());
        log.info("跳转到登录iframe成功");

        WebElement listInput;
        listInput = iframeDriver.findElement(By.xpath("//*[@id=\"m_commonLogin\"]/div[1]/span/input"));
        listInput.sendKeys(username);
        listInput = iframeDriver.findElement(By.xpath("//*[@id=\"m_commonLogin\"]/div[2]/span/input"));
        listInput.sendKeys(password);

        iframeDriver.findElement(By.xpath("//*[@id=\"m_commonLogin\"]/div[5]/a[1]")).click();
        log.info("login success");
    }

}
